package com.woniuxy.java106crm.controller;

import com.woniuxy.java106crm.annotation.RequireMethod;
import com.woniuxy.java106crm.entity.Leads;
import com.woniuxy.java106crm.entity.User;
import com.woniuxy.java106crm.request.Message;
import com.woniuxy.java106crm.request.ResponseResult;
import com.woniuxy.java106crm.request.StateCode;
import com.woniuxy.java106crm.service.MarketingService;
import com.woniuxy.java106crm.service.UserService;
import com.woniuxy.java106crm.vo.marketing.LeadsPageSizeVo;
import com.woniuxy.java106crm.vo.marketing.LeadsStateVo;
import com.woniuxy.java106crm.vo.marketing.LeadsVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;

@Api(tags = "销售机会管理API文档")
@Slf4j
@RestController
@RequestMapping("/marketing")
public class MarketingController {
    @Resource
    private MarketingService marketingService;

    @Resource
    private UserService userService;

    /**
     * 查询数据yi并且分页
     * @return
     */
    @PostMapping("/findAll")
    public ResponseResult findAll(@RequestBody LeadsPageSizeVo leadsPageSizeVo){
        return new ResponseResult(StateCode.SUCCESS, Message.SUCCESS, marketingService.findAll(leadsPageSizeVo));
    }

    /**
     * 创建(增加)销售机会
     */
    @RequireMethod("创建(增加)销售机会")
    @PostMapping("/addLeads")
    public ResponseResult addLeads(@Valid @RequestBody Leads leads, HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        User user = userService.findById(uid);
        leads.setCreator(user.getUsername());
        leads.setAppointStatus("2");
        return new ResponseResult(StateCode.SUCCESS, Message.SUCCESS, marketingService.addLeads(leads));
    }

    /**
     * 编辑(修改)销售机会
     */
    @RequireMethod("编辑(修改)销售机会")
    @PostMapping("/updateLeads")
    public ResponseResult updateLeads(@RequestBody Leads leads){
        return new ResponseResult(StateCode.SUCCESS, Message.SUCCESS, marketingService.updateLeads(leads));
    }

    /**
     * 查询有无指派的权限
     */
    @GetMapping("/haveRole")
    public ResponseResult haveRole(HttpSession session){
        int uid = (int)session.getAttribute("uid");
        return new ResponseResult<>(StateCode.SUCCESS, Message.SUCCESS, marketingService.haveRole(uid));
    }

    /**
     * 删除销售机会
     */
    @RequireMethod("删除销售机会")
    @DeleteMapping("/delLeadsById/{id}/{creator}")
    public ResponseResult delLeadsById(@PathVariable("id") int id, @PathVariable("creator") String creator, HttpSession session){
        // 拿到当前登录用户
        Integer uid = (Integer) session.getAttribute("uid");
        User user = userService.findById(uid);
        String username = user.getUsername();
        if(!username.equals(creator)){
            return new ResponseResult(StateCode.MISTAKE, Message.SUCCESS, "你不能删除别人创建的机会");
        }
        return new ResponseResult(StateCode.SUCCESS, Message.SUCCESS, marketingService.delLeadsById(id));
    }

    /**
     * 根据id查询销售机会
     */
    @RequireMethod("查询销售机会")
    @GetMapping("/findLeadsById/{id}")
    public ResponseResult findLeadsById(@PathVariable("id") int id){
        return new ResponseResult(StateCode.SUCCESS, Message.SUCCESS, marketingService.findLeadsById(id));
    }

    /**
     * 根据id修改指派人和时间
     */
    @RequireMethod("修改指派人和时间")
    @PostMapping("/updateAppointor")
    public ResponseResult updateAppointor(@RequestBody Leads leads){
        LeadsVo leadsVo = new LeadsVo();
        leadsVo.setId(leads.getId());
        leadsVo.setAppointStatus("1");
        leadsVo.setAppointor(leads.getAppointor());
        return new ResponseResult(StateCode.SUCCESS, Message.SUCCESS, marketingService.updateAppointor(leadsVo));
    }

    /**
     * 根据id修改机会状态
     */
    @RequireMethod("修改机会状态")
    @PostMapping("/updateStatById")
    public ResponseResult updateStatById(@RequestBody LeadsStateVo leadsStateVo){
        return new ResponseResult<>(StateCode.SUCCESS, Message.SUCCESS, marketingService.updateStatById(leadsStateVo));
    }

}
